<?php
require_once("Tool.php");

Class NewsList{

  public $newslist;
  public $pageSize;
  
  function __construct($size) {
  	$this->pageSize = $size;
		}	
		
  function getNews($page)
  {
  	require_once("Dbcon.php");
	$dbc = new Dbcon();
  	$first = ($page - 1) * $this->pageSize;
	$last = $this->pageSize;
	
	$sql = "select n.*, a.filename from news n left join attachment a on n.nid = a.nid and a.status = 'true' where n.status = 'true' order by n.updated_date desc LIMIT " . $first . ", ". $last;
	$result = $dbc->getSelect($sql);
	$dbc->clear();	
	return $result;
  }
  
  function getPage()
  {
  	require_once("Dbcon.php");
	$dbc = new Dbcon();
	
	$sql = "select count(*) from news where status = 'true' order by updated_date desc";
	$result = $dbc->getSelect($sql);
	$dbc->clear();	
	
	return ceil(reset(reset($result))/$this->pageSize);
  }
  
  function setNews($subject, $message)
  {
  	require_once("Dbcon.php");
  	$dbc = new Dbcon();
	
	$sql = "insert into news(subject, message, status, created_date, updated_date) values('" . $this->encodeStr($subject) . "', '" . $this->encodeStr($message) . "', 'true', DATE_ADD(NOW(), INTERVAL 15 HOUR),DATE_ADD(NOW(), INTERVAL 15 HOUR))";
	$iid = 0;
	$iid = $dbc->setInsert($sql);
	$dbc->clear();	
	
	return $iid;
  }
  
  function updateNews($nid, $subject, $message)
  {
  	require_once("Dbcon.php");
  	$dbc = new Dbcon();
	
	$sql = "update news set subject = '". $this->encodeStr($subject) . "', message = '" . $this->encodeStr($message) . "' where nid = " . $nid;
	$result = $dbc->setUpdate($sql);
	$dbc->clear();	
	
	return $result;
  }
  
  function delNews($nid)
  {
  	require_once("Dbcon.php");
  	$dbc = new Dbcon();
	
	$sql = "update news set status = 'closed' where nid = " . $nid;
	$result = $dbc->setUpdate($sql);
	$dbc->clear();	
	
	return $result;
  }
  
  function encode($str)
  {
  	return Tool::encode($str);
  }
  
  public function encodeStr($str)
  {
  	return Tool::encode($str);
  }		
}
?>